// page-billOfDocumentImg/updateBillOfDocumentImg/updateBillOfDocumentImg.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    url: app.globalData.requestURL +'upload/uploadImgTest',
    path: '', //文件本地路径
    name: '', //name标识符
    imagesType: '', //自定义上传的目录路径
    identityNum: '', //身份证号
    update_certificate: wx.getStorageSync("update_certificate"), //合格证书或档案证书
    update_invoice: wx.getStorageSync("update_invoice"), //采购发票照片
    update_purchaseContract: wx.getStorageSync("update_purchaseContract"), //采购合同照片
    update_purchaseContractSec: wx.getStorageSync("update_purchaseContractSec"),
    bg_certificate: '', //合格证书或档案证书
    bg_invoice: '', //采购发票照片
    bg_purchaseContract: '', //采购合同照片
    bg_purchaseContractSec: '',//背景合同第二页
    bigShelfNum: '',//设备出厂编号
    qylxr: false,
    isSubmit: true,
    rollOutIdentification: ''//转出人
  },
  chooseImage: function (e) {
    console.log(e.target.id)
    var self = this;
    wx.chooseImage({
      count: 1,
      sourceType: ['camera'],
      success: function (res) {
        if (e.target.id == '0') {
          self.setData({
            name: "purchaseContract",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum + '/' + self.data.bigShelfNum + '/' + "shouxu"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync("update_purchaseContract", imgResponseUrl)
            self.setData({
              update_purchaseContract: wx.getStorageSync("update_purchaseContract")
            })
            if (self.data.update_purchaseContract != '') {
              self.setData({
                bg_purchaseContract: ''
              })
            }
          };
        } else if (e.target.id == '1') {
          self.setData({
            name: "invoice",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum + '/' + self.data.bigShelfNum + '/' + "shouxu"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync("update_invoice", imgResponseUrl)
            self.setData({
              update_invoice: wx.getStorageSync("update_invoice")
            })
            if (self.data.update_invoice != '') {
              self.setData({
                bg_invoice: ''
              })
            }
          };
        } else if (e.target.id == '2') {
          self.setData({
            name: "certificate",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum + '/' + self.data.bigShelfNum + '/' + "shouxu"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync("update_certificate", imgResponseUrl)
            self.setData({
              update_certificate: wx.getStorageSync("update_certificate")
            })
            if (self.data.update_certificate != '') {
              self.setData({
                bg_certificate: ''
              })
            }
          };
        } else if (e.target.id == '3') {
          self.setData({
            name: "purchaseContractSec",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum + '/' + self.data.bigShelfNum + '/' + "shouxu"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync("update_purchaseContractSec", imgResponseUrl)
            self.setData({
              update_purchaseContractSec: wx.getStorageSync("update_purchaseContractSec")
            })
            if (self.data.update_purchaseContractSec != '') {
              self.setData({
                bg_purchaseContractSec: ''
              })
            }
          };
        }
      }
    });
  },
  submit: function (e) {
    var that = this;
    if (that.data.isSubmit) {
      that.setData({
        isSubmit: false
      })
      var identityNum = that.data.identityNum;
      var contractForBuyImg = that.data.update_purchaseContract;
      if (contractForBuyImg == "") {
        contractForBuyImg == that.data.bg_purchaseContract
      }
      var contractSecImg = that.data.update_purchaseContractSec;
      if (contractSecImg == "") {
        contractSecImg == that.data.bg_purchaseContractSect
      }
      var invoiceForBuyImg = that.data.update_invoice;
      if (invoiceForBuyImg == "") {
        invoiceForBuyImg = that.data.bg_invoice
      }
      var archivesImg = that.data.update_certificate;
      if (archivesImg == "") {
        archivesImg = that.data.bg_certificate
      }
      var bigShelfNum = that.data.bigShelfNum;
      wx.request({
        url: app.globalData.requestURL + 'billOfDocumentImg/save',
        method: 'POST',
        data: {
          bigShelfNum: bigShelfNum,
          identityNum: identityNum,
          contractForBuyImg: contractForBuyImg,
          invoiceForBuyImg: invoiceForBuyImg,
          archivesImg: archivesImg,
          contractSecImg: contractSecImg
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          if (res.data.status == '0') {
            wx.setStorageSync('update_purchaseContractSec', '');
            wx.setStorageSync('update_purchaseContract', '');
            wx.setStorageSync('update_invoice', '');
            wx.setStorageSync('update_certificate', '');
            wx.redirectTo({
              url: '/page-wode/deviceInformation/deviceList/deviceList'
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '不可多次提交',
        icon: 'loading',
        duration: 2000,
        mask: true
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      bigShelfNum: wx.getStorageSync("rollOutFactoryNum"),//转出设备的编号
      identityNum: wx.getStorageSync('identification'),//转入单位
      rollOutIdentification: wx.getStorageSync("rollOutIdentification")//转出单位
    });
    var bigShelfNum = that.data.bigShelfNum
    var identityNum = that.data.identityNum
    var rollOutIdentification = that.data.rollOutIdentification

    //查询此照片表是否有提交过照片
    wx.request({
      url: app.globalData.requestURL + 'billOfDocumentImg/getBillOfDocumentByCondition',//等查询此设备的手续的接口,
      method: 'POST',
      data: {
        bigShelfNum: bigShelfNum,
        identityNum: identityNum
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },

      success: function (res) {
        if (res.data.status == '1') {
          wx.redirectTo({
            url: '/page-wode/deviceInformation/deviceList/deviceList'
          })
        } if (res.data.status == '0') {
          wx.request({
            url: app.globalData.requestURL + 'billOfDocumentImg/getNewDeviceImgByEntity',//等查询此设备的手续的接口,
            method: 'POST',
            data: {
              bigShelfNum: bigShelfNum,
              identityNum: rollOutIdentification
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },

            success: function (res2) {
              if (res2.data.status == '0') {
                that.setData({
                  bg_certificate: res2.data.data[0].archivesImg,
                  bg_invoice: res2.data.data[0].invoiceForBuyImg,
                  bg_purchaseContract: res2.data.data[0].contractForBuyImg,
                  bg_purchaseContractSec: res2.data.data[0].contractSecImg,
                });
              }
            }
          })
        }
      }
    })
    if (wx.getStorageSync("update_purchaseContractSec") != '') {
      that.setData({
        bg_purchaseContractSec: ''
      });
    }
    if (wx.getStorageSync("update_certificate") != '') {
      that.setData({
        bg_certificate: ''
      });
    }
    if (wx.getStorageSync("update_invoice") != '') {
      that.setData({
        bg_invoice: ''
      });
    }
    if (wx.getStorageSync("update_purchaseContract") != '') {
      that.setData({
        bg_purchaseContract: ''
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})